<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0023)http://139.196.140.118/ -->
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <title>APIJSON Test</title>
</head>
<body>
<div class="match">
    <div class="top" aria-orientation="horizontal">
        <h2 class="title" onclick="toMainPage()">fly Test</h2>
    </div>
    <div class="center" aria-orientation="horizontal">
        <textarea class="center-left" id="vInput" onkeyup="onChange(true)">        {
            "user":{

                }
            }
        }
        </textarea>
        <textarea class="center-right" id="vOutput" readonly="">        initializing...
        </textarea>
    </div>
    <div class="bottom" aria-orientation="horizontal">
        <button class="button" id="vTransfer" onclick="transfer()">' / "</button>
        <input class="url" id="vUrl" type="text" placeholder="input url here" onkeyup="doOnkeyUp(event)">
        <button class="send" id="vSend" onclick="send()">Send</button>
    </div>
</div>
<style type="text/css">
    html,body,div{
        width: 100%;
        height: 100%;
        position: relative;
        margin: 0;
        padding: 0;
    }
    .match{
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    .top{
        width: 100%;
        height: 40px;
        padding-left: 2%;
        padding-right: 2%;
        margin: 0;
    }
    .title{
        width: auto;
        height: 20px;
        padding: 0;
        text-overline-color: aquamarine;
    }
    .center{
        width: 100%;
        height: 68%;
        margin: 0;
        padding: 0;
    }
    .center-left{
        width: 30%;
        height: 100%;
        margin: 0;
        padding: auto;
    }
    .center-right{
        width: 68%;
        height: 100%;
        margin: 0;
        padding: auto;
    }
    .bottom{
        margin-top: 5px;
        width: 100%;
        height: 40px;
        padding: 0;
    }
    .url{
        width: 63%;
        height: 50px;
        margin: 0;
    }
    .button{
        width: 10%;
        height: 50px;
        margin: 0;
        padding: 0;
    }
    .send{
        width: 25%;
        height: 50px;
        margin: 0;
        padding: 0;
    }
</style>
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="./js/StringUtil.js"></script>
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="./js/CodeUtil.js"></script>
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="./js/RequestUtil.js"></script>
<script type="text/javascript" language="JavaScript" charset="UTF-8" src="./js/ResponseUtil.js"></script>
<script type="text/javascript" language="JavaScript" charset="UTF-8">
  var vInput = document.getElementById("vInput");
  var vOutput = document.getElementById("vOutput");
  var vUrl = document.getElementById("vUrl");
  var vTransfer = document.getElementById("vTransfer");
  var vSend = document.getElementById("vSend");

  vUrl.value = new String(URL_GET);

  /**转至主页
   */
  function toMainPage() {
    window.open('https://github.com/TommyLemon/APIJSON');
  }


  var inputted;
  var handler;
  /**计时回调
   */
  function handle(before) {
    if (inputted != before) {
      clearTimeout(handler);
      return;
    }

    vOutput.value = 'resolving...';

    //格式化输入代码
    try {
      if (before.indexOf("'") >= 0) {
        before = before.replace(/'/g, '"');
      }
      before = format(before);
      const code = getCode(before);

      if (isSingle) {
        if (before.indexOf('"') >= 0) {
          before = before.replace(/"/g, "'");
        }
      }
      else {
        if (before.indexOf("'") >= 0) {
          before = before.replace(/'/g, '"');
        }
      }

      vInput.value = before;
      vSend.disabled = false;
      vOutput.value = "OK, click [Send] button to send a request." + code;
    } catch(e) {
      console.log(e);
      vSend.disabled = true;
      vOutput.value = "Error! Please check and modify JSON request!";
    }
  }

  /**输入内容改变
   */
  function onChange(delay) {
    inputted = new String(vInput.value);
    clearTimeout(handler);

    handler = setTimeout(function () {
      handle(inputted);
    }, delay ? 2*1000 : 0);
  }


  var isSingle = true;
  /**单双引号切换
   */
  function transfer() {
    isSingle = ! isSingle;
    onChange();
  }
  transfer();

  /**发送请求
   */
  function send() {
    clearTimeout(handler);

    var real = new String(vInput.value);
    if (real.indexOf("'") >= 0) {
      real = real.replace(/'/g, "\"");
    }
    var json = JSON.parse(real);

    var url = vUrl.value;
    vOutput.value = "requesting... \nURL = " + url;
    var rq = request(url, json, true, function () {
      if (rq.readyState !== 4) {
        return;
      }

      if (rq.status === 200) {
        var response = rq.responseText;
        if (isSingle) {
          response = formatObject(JSON.parse(rq.responseText));
          response = JSON.stringify(response);
        }
        vOutput.value = format(response);
      } else {
        vOutput.value = "Response(GET):\nurl = " + rq.url + "\nstatus = " + rq.status + "\nerror = " + rq.error;
      }
    });
  }

  /**处理按键事件
   * @param event
   */
  function doOnkeyUp(event) {
    var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
    if (keyCode == 13) { // enter
      send();
    }
  }

  /**转为请求代码
   */
  function getCode(rq) {
    return '\n\n\n\n\n ==================== Java Request ==================== \n' +
      parseJava(null, JSON.parse(rq)) +
      '\n\n\n\n\n ==================== JavaScript Request ==================== \n' + rq;
  }

</script>

</body></html>